14. Source the ROS Environment

Sourcing The ROS Environment

Environment Setup

Before we begin using ROS in a terminal, we must first ensure that all of the environment variables are present. To do this, we must source the setup script provided by ROS:

Caveat:

Make sure you use the bash command source rather than ./ . There’s a subtle distinction between the two commands, in that source executes the script in the current session, while ./ will start a new session, containing a copy of the current environment. When a script executed via ./ is exited, all environment variables set by it will be lost. We don’t want this. For more information on environment variables and terminal sessions, please see here .

Inspecting the Environment

You can inspect the changes that it has made to our environment variables by using the Unix command env .

$ env

Depending on your system configuration, there may be quite a few environment variables. One quick way to determine what changes were made to the existing environment is to run the following commands in a new terminal (where /opt/ros/kinetic/setup.bash has not already been sourced).

The Unix diff command compares two files line by line, indicating any differences between them. The lines beginning with a ‘+’ indicate lines that were either added, or changed in newenv.txt . In our case, we can see that the following changes have been made to the environment by setup.bash

ROS_ROOT=/opt/ros/kinetic/share/ros
ROS_PACKAGE_PATH=/opt/ros/kinetic/share
ROS_MASTER_URI=http://localhost:11311
LD_LIBRARY_PATH=/opt/ros/kinetic/lib
PATH=/opt/ros/kinetic/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
ROSLISP_PACKAGE_DIRECTORIES=
ROS_DISTRO=kinetic
PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages
PKG_CONFIG_PATH=/opt/ros/kinetic/lib/pkgconfig
CMAKE_PREFIX_PATH=/opt/ros/kinetic
ROS_ETC_DIR=/opt/ros/kinetic/etc/ros

While we won’t be discussing all of the environment variables that have been added or changed, there are a few worth mentioning.

ROS_ROOT=/opt/ros/kinetic/share/ros
The path where core ros packages are stored

PATH=/opt/ros/kinetic/bin:...
The path to the ROS binaries, which we will be using throughout this lesson.

ROS_DISTRO=kinetic
Which distribution of ROS is being used. In this case, we are using kinetic

PYTHONPATH=/opt/ros/kinetic/lib/python2.7/dist-packages
The path to the ROS python packages, which we will be using next lesson.

More information about the environment variables mentioned above, the ones not mentioned here, and others can be found here .

Automatically Configuring the Environment

Setting up the ROS environment every time you open a new terminal window can be painful and tedious. To avoid the tedium, we can simply add the command to source the workspace to our ~/.bashrc file . This will cause our environment to be sourced any time a new terminal session is created.

Please note that the provided VM already has this configured so there is no need to execute this step!

Which one of the statements below is true regarding the bash source command?

SOLUTION: Executes the bash script within the existing environment.

Which one of the statements below is true regarding the bash ./ command?

SOLUTION: Environment used by the executed command is destroyed when script is done running

We are now ready to begin our next concept!